'use strict';

/* Controllers */

angular.module('myApp.controllersGame', [])
        .controller('GameSingleCtrl', ['$scope', '$rootScope', 'serviceGame', 'serviceStorage',
            function($scope, $rootScope, serviceGame, serviceStorage) {
                /**
                 * start after content id loaded
                 */
                $scope.$on('$viewContentLoaded', function() {

                    var game = serviceGame.createGameFromStorage();
                    
                    serviceGame.clear();
                    serviceGame.nextMoveValue = game.value;
                    serviceGame.singleOrMulti = 'single';
                    serviceGame.playerValue = game.value;
                    
                    var oponentValue = game.getOponentValue();
                    game.setValueCallback = function(gameArray, x, y, value) {
                        
                        serviceGame.movesCount++;
                        
                        game.setMoveLocked(true);
                        
                        // check if player winn
                        serviceGame.winnerValue = game.getWinnerValue();
                        if (serviceGame.winnerValue !== null) {
                            
                            game.drawSurface();
                            console.log('gra zakonczona, wygral PLAYER wartosc : ', serviceGame.winnerValue);
                            $rootScope.$apply();
                            
                        } else {
                            
                            serviceGame.nextMoveValue = game.getOponentValue();
                            $rootScope.$apply();
                            
                            //var worker = new Worker("worker_ai.js");
                            
                            var onmessage = function(event) {
                                var response = event.data;
                                console.log(response);
                                game.setOponentValue(response.move.x, response.move.y, oponentValue);
                                game.setMoveLocked(false);
                                
                                // check if AI win
                                serviceGame.winnerValue = game.getWinnerValue();
                                if (serviceGame.winnerValue !== null) {

                                    game.drawSurface();
                                    game.setMoveLocked(true);
                                    console.log('gra zakonczona, wygral AI wartosc : ', serviceGame.winnerValue);
                                }
                                serviceGame.nextMoveValue = game.value;
                                serviceGame.movesCount++;
                                $rootScope.$apply();
                            };

                            var onerror = function(error) {
                                console.log(error.message);
                            };

                            var worker = JsWorker.createWorkerFromUrl("worker_ai.js", onmessage, onerror);

                            worker.postMessage({
                                'gameArrayOfArray' : game.getGame(),
                                'oponentValue' : oponentValue
                            });
  
                        } 
                    };
                    game.init();
                    serviceGame.startTimer();
                    
                    var event = document.createEvent('Event');
                    event.initEvent('gamestart', true, true);
                    document.dispatchEvent(event);
                    
                });
            }
        ])
        .controller('GameMultiCtrl', ['$scope', '$rootScope', 'serviceGame', 'serviceStorage',
            function($scope, $rootScope, serviceGame, serviceStorage) {
                /**
                 * start after content id loaded
                 */
                $scope.$on('$viewContentLoaded', function() {

                    var game = serviceGame.createGameFromStorage();
                    
                    serviceGame.clear();
                    serviceGame.nextMoveValue = game.value;
                    serviceGame.singleOrMulti = 'multi';                    
                    
                    game.setValueCallback = function(gameArray, x, y, value) {
                        serviceGame.movesCount++;
                        serviceGame.winnerValue = game.getWinnerValue();

                        if (serviceGame.winnerValue === null) {
                            game.value = game.getOponentValue();
                            
                            serviceGame.nextMoveValue = game.value;
                        } else {
                            game.drawSurface();
                            game.setMoveLocked(true);
                            console.log('gra zakonczona, wygrala wartosc : ', serviceGame.winnerValue);
                        }

                        $rootScope.$apply();
                    };
                    game.init();
                    serviceGame.startTimer();
                    
                    var event = document.createEvent('Event');
                    event.initEvent('gamestart', true, true);
                    document.dispatchEvent(event);
                    
                });
            }
        ])
        .controller('GameQuitCtrl', ['$scope', '$location', 'serviceGame', 'serviceStats',
            function($scope, $location, serviceGame, serviceStats) {
                
                var redirectTo = $location.search().url;
                if (typeof redirectTo === 'undefined') {
                    redirectTo = '/main';
                }
                
                serviceStats.persist(serviceGame);
                serviceGame.clear();
                $location.path(redirectTo).search('url', null);
            }
        ]);